﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Register.Command;
using Register.IDAL;
using Register.Model;
using Register.DBUtility;
namespace Register.DAL
{
    /// <summary> 
    /// FileName: DoctorUserInfoDAL.cs    
    /// CLRVersion: 4.0.30319.269    
    /// Author: DoubleZ    
    /// Corporation:     
    /// Description:     
    /// DateTime: 2012/6/29 17:10:54    
    /// </summary>
  public   class DoctorUserInfoDAL:IDoctorUserDALInfo
  {
      private string  tableName = "System_doctorUserInfo";
      #region  添加医生用户
      public int AddDoctorUserInfo(Model.DoctorUserInfo model) {
          StringBuilder strSql = new StringBuilder();
          strSql.Append("insert into System_doctorUserInfo(");
          strSql.Append("DoctorUser_ID,Doctor_ID,DoctorUser_UserName,DoctorUser_Password,DoctorUser_AddTime,DoctorUser_Recoder,DoctorUser_State)");
          strSql.Append(" values (");
          strSql.Append("@DoctorUser_ID,@Doctor_ID,@DoctorUser_UserName,@DoctorUser_Password,@DoctorUser_AddTime,@DoctorUser_Recoder,@DoctorUser_State)");
          SqlParameter[] parameters = {
					new SqlParameter("@DoctorUser_ID", SqlDbType.Char,14),
					new SqlParameter("@Doctor_ID", SqlDbType.Char,14),
					new SqlParameter("@DoctorUser_UserName", SqlDbType.VarChar,20),
					new SqlParameter("@DoctorUser_Password", SqlDbType.Char,32),
					new SqlParameter("@DoctorUser_AddTime", SqlDbType.DateTime),
					new SqlParameter("@DoctorUser_Recoder", SqlDbType.VarChar,20),
					new SqlParameter("@DoctorUser_State", SqlDbType.Char,1)
                                      };
          parameters[0].Value = model.DoctorUser_ID;
          parameters[1].Value = model.Doctor_ID;
          parameters[2].Value = model.DoctorUser_UserName;
          parameters[3].Value = model.DoctorUser_Password;
          parameters[4].Value = model.DoctorUser_AddTime;
          parameters[5].Value = model.DoctorUser_Recoder;
          parameters[6].Value = model.DoctorUser_State;
        
        int flag =(int)DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
          //如果添加成功，更新医生表中的医生用户ID
        if (flag > 0)
        {

            string strSql1 = "update System_doctorInfo set DoctorUser_ID='" + model.DoctorUser_ID + "' where Doctor_ID='" + model.Doctor_ID + "'";
            return DbHelperSQL.ExecuteSql(strSql1);
        }
        else return -1;
          
      }
      #endregion
      #region  根据医生ID获取医生姓名
      public string GetDoctorNameById(string doctorID) { 
        string strSql = "select Doctor_Name from System_doctorInfo where Doctor_ID='"+doctorID+"'";
     DataSet ds = DbHelperSQL.Query(strSql);
     string doctorName = "无";
     if (ds != null)
     {
         doctorName= ds.Tables[0].Rows[0]["Doctor_Name"].ToString();

     }
     return doctorName;
        }
   
      #endregion
      #region 获取医生用户信息
      public List<Model.DoctorUserInfo> GetPagedDoctorUserInfo(int size, int pageIndex, string where)
      {
          return DataSetToList(DbHelperSQL.getPagedData(" * ", "DoctorUser_AddTime", where, tableName, OrderBy.DESC, pageIndex, size));
      }

      public     int GetDoctorUserInfoCount(string where)
      {
          int count = 0;
          StringBuilder str = new StringBuilder();
          str.Append(" select count(*) from ");
          str.Append(tableName);
          if (where != null)
          {
              str.Append(" where ");
              str.Append(where);
              count = (int)DbHelperSQL.GetSingle(str.ToString());
          }
          else
          {
              count = (int)DbHelperSQL.GetSingle(str.ToString());
          }
          return count;
      }
      private List<Model.DoctorUserInfo> DataSetToList(DataSet ds)
      {
          List<Model.DoctorUserInfo> list = new List<Model.DoctorUserInfo>();
          foreach (DataRow dr in ds.Tables[0].Rows)
          {
              DoctorUserInfo doctor = new DoctorUserInfo();
              doctor.Doctor_ID = (string)dr["Doctor_ID"];
              doctor.DoctorUser_AddTime = (DateTime)dr["DoctorUser_AddTime"];
              doctor.DoctorUser_ID = (string)dr["DoctorUser_ID"];
              doctor.DoctorUser_Password = (string)dr["DoctorUser_Password"];
              doctor.DoctorUser_Recoder = (string)dr["DoctorUser_Recoder"];
              doctor.DoctorUser_UserName = (string)dr["DoctorUser_UserName"];

              list.Add(doctor);
          }
          return list;
      }
      #endregion

      #region 删除医生用户
      public   int DeleteDoctorUserInfo(String doctorUserID) {
       StringBuilder strSql = new StringBuilder();
       strSql.Append("delete from System_doctorUserInfo ");
       strSql.Append(" where DoctorUser_ID=@DoctorUser_ID ");
       SqlParameter[] parameters = {
					new SqlParameter("@DoctorUser_ID", SqlDbType.Char,14)};
       parameters[0].Value = doctorUserID;

        int flag=DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        if (flag > 0)
        {
            string strSql1 = "update System_doctorInfo set DoctorUser_ID=null where DoctorUser_ID='" + doctorUserID + "'";
            return DbHelperSQL.ExecuteSql(strSql1.ToString());
        }
        else return -1;
      }
      #endregion
      #region 更新医生用户
  public int UpdateDoctorUserInfo(DoctorUserInfo doctorUserInfo) { 
    StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_doctorUserInfo set ");
            strSql.Append("DoctorUser_UserName=@DoctorUser_UserName,");
            strSql.Append("DoctorUser_Password=@DoctorUser_Password,");
            strSql.Append("DoctorUser_LastAmendPerson=@DoctorUser_LastAmendPerson, ");
            strSql.Append("DoctorUser_LastAmendTime=@DoctorUser_LastAmendTime");
            
            strSql.Append(" where DoctorUser_ID=@DoctorUser_ID");
            SqlParameter[] parameters = {	
					new SqlParameter("@DoctorUser_UserName", SqlDbType.VarChar,20),
					new SqlParameter("@DoctorUser_Password", SqlDbType.Char,32),
					new SqlParameter("@DoctorUser_LastAmendPerson", SqlDbType.VarChar,20),
					new SqlParameter("@DoctorUser_LastAmendTime", SqlDbType.DateTime),
					
                    new SqlParameter("@DoctorUser_ID", SqlDbType.Char,14)
                                       };

            parameters[0].Value = doctorUserInfo.DoctorUser_UserName;

            parameters[1].Value = doctorUserInfo.DoctorUser_Password;
            parameters[2].Value = doctorUserInfo.DoctorUser_LastAmendPerson;
            parameters[3].Value = doctorUserInfo.DoctorUser_LastAmendTime;
          
            parameters[4].Value = doctorUserInfo.DoctorUser_ID;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
   
      #endregion
      #region 检查医生用户是否已经存在
 public  bool Exists(string doctorUserName) { 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from System_doctorUserInfo");
            strSql.Append(" where DoctorUser_UserName=@DoctorUser_UserName ");
            SqlParameter[] parameters = {
					new SqlParameter("@DoctorUser_UserName", SqlDbType.VarChar,20)};
            parameters[0].Value = doctorUserName;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

  
      #endregion

  }

}
